package com.example.origin.demo.design.builder;

import java.util.ArrayList;
import java.util.List;


/**
 * 解决：将变与不变分离开
 * @author yutao
 * @create 2019-12-31 10:08
 **/
public class BuilderDemo {
    public static void main(String[] args) {
        Person xiaoming = Person.builder().name("xiaoming").age(11).build();
        System.out.println(xiaoming.toString());
    }
}
class Person {

    private String name;

    private int age;



    private Person(String name, int age) {

        this.name = name;

        this.age = age;

    }



    public static PersonBuilder builder() {

        return new PersonBuilder();

    }



    public static class PersonBuilder {

        private String name;

        private int age;



        public PersonBuilder name(String name) {

            this.name = name;

            return this;

        }



        public PersonBuilder age(int age) {

            this.age = age;

            return this;

        }



        public Person build() {

            return new Person(name, age);

        }

    }

}



